$(function () {
    /*发送第一句话*/
    createInfo('service', '您好');

    // 记录时间
    timeRecord();
});

var d = document;

// 模拟客服回复内容
var serviceData = {
    'robot': {
        'chat': ['您好', '好的', '稍等哦~', '不客气', '嗯嗯'],
    }
};

var chatHint = $('#chatHint'),
    chatInput = d.querySelector('#chat_input'),
    chatContain = d.querySelector('#chat_contain');


function timeRecord() {
    var chatTime = new Date(); //当前时间
    var nowYear = chatTime.getFullYear();
    var nowMonth = chatTime.getMonth() + 1;
    var nowDate = chatTime.getDate();
    var nowHours = chatTime.getHours();
    var nowMinutes = chatTime.getMinutes();

    var timeRecord = $('.time-record');
    timeRecord = nowYear + "-" + nowMonth + "-" + nowDate + "  " + nowHours + ":" + zeroFill(nowMinutes);
    $('.time-record').text(timeRecord);
}

/* 创建新消息*/
function createInfo(name, value) {
    // 客服名、用户名
    var serviceName = "客服01";
    var youName = "王老五";

    var nodeDiv = d.createElement('div'),
        nodeName = d.createElement('p'),
        nodeSpan = d.createElement('span');

    value = value.replace(/(((ht|f)tps?):\/\/)?([A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*)/g, function (content) {
        return "<a href='http://" + content + "' class='chat-address' target='view_window' style='color:#6666cc '>" + content + '</a>';;
    });

    nodeDiv.classList.add('chat-' + name + '-contain');
    nodeSpan.classList.add('chat-' + name + '-text', 'chat-text');
    nodeName.classList.add('chat-name');

    nodeSpan.innerHTML = value;
    nodeName.innerHTML = name == 'service' ? serviceName : youName;

    nodeDiv.appendChild(nodeName);
    nodeDiv.appendChild(nodeSpan);
    chatContain.appendChild(nodeDiv);
    chatContain.scrollTop = chatContain.scrollHeight;
}

// 时间补零方法
function zeroFill(value) {
    return value >= 10 ? value : "0" + value;
}

var timer,
    timerId,
    flagInput = false,
    shiftDown = false; // 判断是否按住shift键

// 空输入提示
function chatHintNull(chatHint) {
    setTimeout(function () {
        chatHint.fadeIn();
        clearTimeout(timerId);
        timer = setTimeout(function () {
            chatHint.fadeOut();
        }, 1000);
    }, 10);
    timerId = timer;
}

/* 监控是否按下enter */
function isEnter(Input, Hint, type, e) {
    e = e || window.event;
    if (e.keyCode == 16) { //按住shift键
        shiftDown = true;
    }
    if (e.keyCode == 13) {
        if (shiftDown == true) {
            shiftDown = false;
            return true;
        } else if (shiftDown == false && Input.value == '') {
            Hint();
            return true;
        } else {
            e.preventDefault();
            createInfo(type, Input.value);
            submityouText(Input.value);
            Input.value = null;
            Input.focus();
        }
    }
}

/* 输入框按enter */
chatInput.addEventListener('keydown', function (e) {
    e = e || window.event;
    isEnter(chatInput, chatHintNull, 'you', e);
})

/* 点击发送信息 */
$('.chat-input-button').click(function () {
    if (chatInput.value != '') {
        createInfo('you', chatInput.value);
        submityouText(chatInput.value);
        chatInput.value = null;
        chatInput.focus();
    } else {
        chatHintNull(chatHint);
    }
});

// 模拟客服回复
function submityouText(text) {
    var num = Math.random() * 10;
    if (num <= 7) {
        getServiceText(serviceData);
    }
}

/* 发送客服消息 */
function getServiceText(data) {
    var serviceText = data.robot.chat,
        i = Math.floor(Math.random() * serviceText.length);
    createInfo('service', serviceText[i]);
}